package LeetCode.leetcode404;

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode() {
    }

    TreeNode(int val) {
        this.val = val;
    }

    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

public class Solution {
    public int sumOfLeftLeaves(TreeNode root) {
        if (root == null)
            return 0;
        return dfs(root);
    }

    public static int dfs(TreeNode node) {
        if (node == null)
            return 0;
        //找到左叶子结点
        int sum = 0;
        if (node.left != null) {
            if (checkLeaves(node.left)) {
                sum += node.left.val;
            }else {
                sum += dfs(node.left);
            }
        }
        if (node.right != null) {
            sum += dfs(node.right);
        }
        return sum;
    }

    public static boolean checkLeaves(TreeNode node) {
        if (node.left == null && node.right == null)
            return true;
        else
            return false;
    }

}
